package cn.itcast.orders.mapper;

import cn.itcast.orders.dto.GoodsSalesDTO;
import cn.itcast.orders.dto.OrdersPageQueryDTO;
import cn.itcast.orders.pojo.Orders;
import cn.itcast.orders.vo.OrderStatisticsVO;
import cn.itcast.orders.vo.OrderVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 新增订单
     * @param orders
     */
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status)" +
            " values (#{number}, #{status}, #{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    void insert(Orders orders);


    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据订单状态查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<OrderVO> selectQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单及其详情信息
     * @param id
     * @return
     */
    OrderVO getOrderDetailById(Long id);

    /**
     * 获取各个状态的订单数量统计
     * @return
     */
    OrderStatisticsVO getStatistics();

    /**
     * 按状态和时间查询
     * @param status
     * @param time
     * @return
     */
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);

    /**
     * 根据条件获取订单营业额
     * @param map
     * @return
     */
    Double getTurnoverReportByMap(Map<String, Object> map);

    /**
     * 根据下单时间和状态获取订单
     * @param map
     * @return
     */
    Integer getOrdersReportByMap(Map<String, Object> map);

    /**
     * 获取商品销量前10
     * @return
     * @param map
     */
    List<GoodsSalesDTO> getSalesTop10(Map<String, Object> map);

    /**
     * 查询订单业务数据
     * @param map
     * @return
     */
    @MapKey("")
    List<Map<String, Object>> getBusinessDataList(Map<String, Object> map);
}
